package com.umeng.analytics.pro;

import android.text.TextUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class bw {
    public static final String a = System.getProperty("line.separator");
    private static final String b = "helper";

    public bw() {

    }

    public static void a(File arg1, String arg2) throws IOException {
        bw.a(arg1, arg2.getBytes());
    }

    public static void a(File arg2, byte[] arg3) throws IOException {
        FileOutputStream v1 = new FileOutputStream(arg2);
        try {
            v1.write(arg3);
            v1.flush();
        } catch (Throwable v0) {
            bw.a(v1);
            throw v0;
        }

        bw.a(v1);
    }

    public static String a(String arg7) {
        String v0_1;
        int v0 = 0;
        if (arg7 == null) {
            v0_1 = null;
            return v0_1;
        }

        try {
            byte[] v1 = arg7.getBytes();
            MessageDigest v2 = MessageDigest.getInstance("MD5");
            v2.reset();
            v2.update(v1);
            v1 = v2.digest();
            StringBuffer v2_1 = new StringBuffer();
            while (v0 < v1.length) {
                v2_1.append(String.format("%02X", Byte.valueOf(v1[v0])));
                ++v0;
            }

            v0_1 = v2_1.toString();
        } catch (Exception v0_2) {
            v0_1 = arg7.replaceAll("[^[a-z][A-Z][0-9][.][_]]", "");
        }

        return v0_1;
    }

    public static String a(InputStream arg5) throws IOException {
        InputStreamReader v0 = new InputStreamReader(arg5);
        char[] v1 = new char[1024];
        StringWriter v2 = new StringWriter();
        while (true) {
            int v4 = v0.read(v1);
            if (-1 == v4) {
                break;
            }

            v2.write(v1, 0, v4);
        }

        return v2.toString();
    }

    public static String a(String arg5, int arg6) {
        int v2;
        String v1_2;
        int v1_1;
        String v0 = "";
        try {
            if (TextUtils.isEmpty(arg5)) {
                return v0;
            }

            if (arg5.length() < arg6) {
                v1_1 = arg5.length();
            } else {
                v1_1 = arg6;
            }
            v1_2 = arg5.substring(0, v1_1);
            v2 = arg6;
            String v4 = v1_2;
            v1_1 = v1_2.getBytes("UTF-8").length;
            v0 = v4;
            while (true) {
                if (v1_1 <= arg6) {
                    return v0;
                }

                --v2;
                try {
                    if (v2 > arg5.length()) {
                        v1_1 = arg5.length();
                    } else {
                        v1_1 = v2;
                    }
                    v1_2 = arg5.substring(0, v1_1);
                } catch (Exception v1) {
                    break;
                }

                try {
                    v0 = v1_2;
                    v1_1 = v1_2.getBytes("UTF-8").length;
                    continue;
                } catch (Exception v0_1) {
                    break;
                }
            }
        } catch (Exception v0_1) {
            by.e(((Throwable) v0_1));
        }
        return v0;
    }

    public static String a(File arg7) {
        MessageDigest v1;
        byte[] v0 = new byte[1024];
        try {
            if (!arg7.isFile()) {
                String v0_2 = "";
                return v0_2;
            }

            v1 = MessageDigest.getInstance("MD5");
            FileInputStream v2 = new FileInputStream(arg7);
            while (true) {
                int v3 = v2.read(v0, 0, 1024);
                if (v3 == -1) {
                    break;
                }

                v1.update(v0, 0, v3);
            }

            v2.close();
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            return null;
        }

        return String.format("%1$032x", new BigInteger(1, v1.digest()));

    }

    public static void a(OutputStream arg1) {
        if (arg1 != null) {
            try {
                arg1.close();
            } catch (Exception v0) {
            }
        }
    }

    public static String b(String arg4) {
        String v0_3;
        try {
            MessageDigest v0_1 = MessageDigest.getInstance("MD5");
            v0_1.update(arg4.getBytes());
            byte[] v1 = v0_1.digest();
            StringBuffer v2 = new StringBuffer();
            int v0_2;
            for (v0_2 = 0; v0_2 < v1.length; ++v0_2) {
                v2.append(Integer.toHexString(v1[v0_2] & 255));
            }

            v0_3 = v2.toString();
        } catch (NoSuchAlgorithmException v0) {
            by.c("helper", "getMD5 error", ((Throwable) v0));
            v0_3 = "";
        }

        return v0_3;
    }

    public static byte[] b(InputStream arg4) throws IOException {
        ByteArrayOutputStream v0 = new ByteArrayOutputStream();
        byte[] v1 = new byte[1024];
        while (true) {
            int v3 = arg4.read(v1);
            if (-1 == v3) {
                break;
            }

            v0.write(v1, 0, v3);
        }

        return v0.toByteArray();
    }

    public static String b(File arg4) {
        String v0_2;
        String v1_2;
        InputStream v1_1;
        Throwable v0_1;
        InputStream v2_1;
        FileInputStream fis = null;
        InputStream v0 = null;
        try {
            if (arg4.exists()) {
                try {
                    fis = new FileInputStream(arg4);
                    byte[] v3 = new byte[fis.available()];
                    fis.read(v3);
                    return new String(v3);
                } catch (Exception e) {

                } finally {
                    bw.c(fis);
                }
            }
        } catch (Throwable v1) {
        }
        return null;
    }

    public static void c(InputStream arg1) {
        if (arg1 != null) {
            try {
                arg1.close();
            } catch (Exception v0) {
            }
        }
    }
}

